﻿using System;
using System.Collections.Generic;

namespace ExcelConfig.Config
{
    public class ExcelRow
    {
        internal IDictionary<string, object> row { get; set; }
        public ExcelRow(object row)
        {
            this.OK = false;
            this.row = new Dictionary<string, object>();
            if (row != null)
            {
                var r = row as IDictionary<string, object>;
                if (r != null)
                {
                    this.row = r;
                    this.OK = true;
                }
            }
        }

        public bool OK { get; internal set; }
        public string Text(string name)
        {
            if (!row.ContainsKey(name) || row[name] == null) return string.Empty;
            var r = row[name];
            if (r != null) if (r is string val) return val; else return r.ToString();
            return string.Empty;
        }
        public decimal Number(string name)
        {
            if (decimal.TryParse(Text(name), out var num)) return num;

            return 0;
        }
        public Guid Id(string name)
        {
            try
            {
                return new Guid(Text(name));
            }
            catch { return Guid.Empty; }
        }
        public DateTime Date(string name)
        {
            if (DateTime.TryParse(Text(name), out var date)) return date;
            return DateTime.MinValue;
        }
    }
}
